import { Injectable } from '@nestjs/common';
import {PostsEntity,} from './posts.entity'
import {InjectRepository} from '@nestjs/typeorm'
import {UserEntity} from '../user/user.entity'
import {Repository} from 'typeorm'
@Injectable()
export class PostsService {
  constructor(
    @InjectRepository(PostsEntity)
    private readonly postRepository:Repository<PostsEntity>,
    @InjectRepository(UserEntity)
    private readonly userRepository:Repository<UserEntity>
  ){}
 
  //添加文章，关联用户
  async add(data:any){
     const {userld} = data
     const user = await this.userRepository.findOne(userld)
     data.user = user
     await this.postRepository.save(data)
     return '文章添加成功'
  }

  async delete(data:any){
    const {id} = data
    const result = await this.postRepository.findOne(id)
    await this.postRepository.remove(result)
    return '删除文章成功'
  }

  async update(data:any){
    const {id,content} = data
    const post = await this.postRepository.findOne(id)
    post.content = content
    await this.postRepository.save(post)
    return '修改文章成功'
  }
}
